const Controller = require('egg').Controller;

class UsersController extends Controller {
    success (data) {
        this.ctx.body = {
            data,
            status: true
        }
    }

    // GET /users
    async index () {
        const users = await this.ctx.model.User.findAll({})
        this.success(users)
    }

    // GET /users/:id
    async show () {
        const id = this.ctx.params.id
        const user = await this.ctx.model.User.find(id)
        this.success(user)
    }

    async edit () {

    }

    // PUT /users/:id/update
    async update () {
        const id = this.ctx.params.id
        const user = await this.ctx.model.User.find(id)
        const result = await user.update(this.ctx.request.body)
        this.success(result)
    }

    // POST /users
    async create () {
        const result = await this.ctx.model.User.create(this.ctx.request.body)
        this.success(result)
    }

    async new () {

    }

    // DELETE /users/id
    async destroy () {
        const id = this.ctx.params.id
        const user = await this.ctx.model.User.find(id)
        this.success(user.destroy())
    }

}

module.exports = UsersController;